﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XStudio.Mvc.Core.Helper
{
    public class JSValidator
    {
        public JSValidator(string funname)
        {
            mFunctionName = funname;
        }
        private string mFunctionName;
        private StringBuilder mScript = new StringBuilder();
        public Validator Create(string control, string tipcontrol)
        {
            Validator item = new Validator(control, tipcontrol);
            item.ShowErrorTip = ShowErrorTip;
            return item;
        }
        public Validator Create(string control)
        {
            return Create(control, null);
        }
        public JSValidator Add(params Validator[] items)
        {
            Items.AddRange(items);
            return this;
        }
        public bool ShowErrorTip
        {
            get;
            set;
        }
        private List<Validator> mItems = new List<Validator>();
        public List<Validator> Items
        {
            get
            {
                return mItems;
            }
        }
        private void WriteLine(string value)
        {
            mScript.AppendLine(value);
        }
        private void WriteLine(string format, params object[] values)
        {
            mScript.AppendLine(string.Format(format, values));
        }
        public override string ToString()
        {
            mScript = new StringBuilder();
            WriteLine("<script language='javascript'>");
            foreach (Validator item in Items)
            {
                mScript.AppendLine(item.ToString());
            }
            WriteLine("function {0}()", mFunctionName);
            WriteLine("{");
            WriteLine("\tvar _msg = '';");
            WriteLine("\tvar _success = true;");
            foreach (Validator item in Items)
            {
                WriteLine("\t_{0}.Do();", item.GetHashCode());
            }
            foreach (Validator item in Items)
            {
                WriteLine("\t _success = _success && _{0}.Success;", item.GetHashCode());
                WriteLine("\tif(_{0}.Message !=null)", item.GetHashCode());
                WriteLine("\t\t_msg+=_{0}.Message;", item.GetHashCode());
            }
            WriteLine("\tif(!_success&&_msg!=''){alert(_msg);}");
            WriteLine("\treturn _success");
            WriteLine("}");
            WriteLine("$(document).ready(function(){");
            foreach (Validator item in Items)
            {
                WriteLine("$('#" + item.Control + "').change(function(){_" + item.GetHashCode() + ".Do();});\r\n");
            }
            WriteLine("});");
            WriteLine("</script>");
            return mScript.ToString();
        }
    }
    public class Validator
    {
        public Validator(string control, string tipcontrol)
        {
            mControl = control;
            mTipControl = tipcontrol;
            WriteLine("var _{0}= new Validator();", GetHashCode());
            WriteLine("_{0}.Execute=function()", GetHashCode());
            WriteLine("{");
        }
        private string mControl;
        private string mTipControl;
        public string Control
        {
            get
            {
                return mControl;
            }
        }
        public bool ShowErrorTip
        {
            get;
            set;
        }
        private StringBuilder mScript = new StringBuilder();
        protected void WriteLine(string value)
        {
            mScript.AppendLine(value);
        }
        protected void WriteLine(string format, params object[] values)
        {
            mScript.AppendLine(string.Format(format, values));
        }
        private void SetErrorMessage(string msg)
        {
            //removeClass,addClass
            WriteLine("\t\t\t$('#{0}').attr('title','{1}');", mControl, msg);
            if (string.IsNullOrEmpty(mTipControl))
            {
                WriteLine("\t\t\t this.Message='{0}\\r\\n';", msg);
            }
            else
            {
                if (ShowErrorTip)
                    WriteLine("\t\t\t$('#{0}').text('{1}');", mTipControl, msg);
                WriteLine("\t\t\t$('#{0}').removeClass('SuccessTip');", mTipControl);
                WriteLine("\t\t\t$('#{0}').addClass('ErrorTip');", mTipControl);
            }
        }
        private void SetSuccessMessage(string msg)
        {
            WriteLine("\t\t\t$('#{0}').attr('title','{1}');", mControl, "");
            if (!string.IsNullOrEmpty(mTipControl))
            {
                WriteLine("\t\t\t$('#{0}').text('{1}');", mTipControl, "");
                if (ShowErrorTip)
                    WriteLine("\t\t\t$('#{0}').text('{1}');", mTipControl, msg);
                WriteLine("\t\t\t$('#{0}').removeClass('ErrorTip');", mTipControl);
                WriteLine("\t\t\t$('#{0}').addClass('SuccessTip');", mTipControl);
            }
        }
        public Validator NotNull(string errmsg, string successmsg)
        {
            WriteLine("\tif(_{0}.Success)", GetHashCode());
            WriteLine("\t{");
            WriteLine("\t\t_{0}.NotNull($('#{1}').val());", GetHashCode(), mControl);
            WriteLine("\t\tif(!_{0}.Success)", GetHashCode());
            WriteLine("\t\t{");
            SetErrorMessage(errmsg);
            WriteLine("\t\t}");
            WriteLine("\t\telse{");
            SetSuccessMessage(successmsg);
            WriteLine("\t\t}");
            WriteLine("\t}");
            return this;
        }
        public Validator DateRegion(string max, string min, string errmsg, string successmsg)
        {
            WriteLine("\tif(_{0}.Success)", GetHashCode());
            WriteLine("\t{");
            WriteLine("\t\t_{0}.DataRegion($('#{1}').val(),'{2}','{3}');", GetHashCode(), mControl, min, max);
            WriteLine("\t\tif(!_{0}.Success)", GetHashCode());
            WriteLine("\t\t{");
            SetErrorMessage(errmsg);
            WriteLine("\t\t}");
            WriteLine("\t\telse{");
            SetSuccessMessage(successmsg);
            WriteLine("\t\t}");
            WriteLine("\t}");
            return this;
        }
        public Validator NumberRegion(string max, string min, string errmsg, string successmsg)
        {
            WriteLine("\tif(_{0}.Success)", GetHashCode());
            WriteLine("\t{");
            WriteLine("\t\t_{0}.NumberRegion($('#{1}').val(),'{2}','{3}');", GetHashCode(), mControl, min, max);
            WriteLine("\t\tif(!_{0}.Success)", GetHashCode());
            WriteLine("\t\t{");
            SetErrorMessage(errmsg);
            WriteLine("\t\t}");
            WriteLine("\t\telse{");
            SetSuccessMessage(successmsg);
            WriteLine("\t\t}");
            WriteLine("\t}");
            return this;
        }
        public Validator LengthRegion(string max, string min, string errmsg, string successmsg)
        {
            WriteLine("\tif(_{0}.Success)", GetHashCode());
            WriteLine("\t{");
            WriteLine("\t\t_{0}.LengthRegion($('#{1}').val(),'{2}','{3}');", GetHashCode(), mControl, min, max);
            WriteLine("\t\tif(!_{0}.Success)", GetHashCode());
            WriteLine("\t\t{");
            SetErrorMessage(errmsg);
            WriteLine("\t\t}");
            WriteLine("\t\telse{");
            SetSuccessMessage(successmsg);
            WriteLine("\t\t}");
            WriteLine("\t}");
            return this;
        }
        public Validator EMail(string errmsg, string successmsg)
        {
            WriteLine("\tif(_{0}.Success)", GetHashCode());
            WriteLine("\t{");
            WriteLine("\t\t_{0}.EMail($('#{1}').val());", GetHashCode(), mControl);
            WriteLine("\t\tif(!_{0}.Success)", GetHashCode());
            WriteLine("\t\t{");
            SetErrorMessage(errmsg);
            WriteLine("\t\t}");
            WriteLine("\t\telse{");
            SetSuccessMessage(successmsg);
            WriteLine("\t\t}");
            WriteLine("\t}");
            return this;
        }
        public Validator Regex(string regex, string errmsg, string successmsg)
        {
            WriteLine("\tif(_{0}.Success)", GetHashCode());
            WriteLine("\t{");
            WriteLine("\t\t_{0}.MatchRegex($('#{1}').val(),'{2}');", GetHashCode(), mControl, regex);
            WriteLine("\t\tif(!_{0}.Success)", GetHashCode());
            WriteLine("\t\t{");
            SetErrorMessage(errmsg);
            WriteLine("\t\t}");
            WriteLine("\t\telse{");
            SetSuccessMessage(successmsg);
            WriteLine("\t\t}");
            WriteLine("\t}");
            return this;
        }
        public Validator SelectCheckbox(int selectcount, string errmsg, string successmsg, params string[] controls)
        {
            WriteLine("\tif(_{0}.Success)", GetHashCode());
            WriteLine("\t{");
            WriteLine("\t\t_{0}.SelectCheckbox($('#{1}').val(),'{2}');", GetHashCode(), mControl, selectcount);
            WriteLine("\t\tif(!_{0}.Success)", GetHashCode());
            WriteLine("\t\t{");
            SetErrorMessage(errmsg);
            WriteLine("\t\t}");
            WriteLine("\t\telse{");
            SetSuccessMessage(successmsg);
            WriteLine("\t\t}");
            WriteLine("\t}");
            return this;
        }
        public Validator StringCompare(string rc, CompareType type, string errmsg, string successmsg)
        {
            WriteLine("\tif(_{0}.Success)", GetHashCode());
            WriteLine("\t{");
            WriteLine("\t\t_{0}.StringCompare($('#{1}').val(),$('#{2}').val(),'{3}');", GetHashCode(), Control, rc, type.ToString());
            WriteLine("\t\tif(!_{0}.Success)", GetHashCode());
            WriteLine("\t\t{");
            SetErrorMessage(errmsg);
            WriteLine("\t\t}");
            WriteLine("\t\telse{");
            SetSuccessMessage(successmsg);
            WriteLine("\t\t}");
            WriteLine("\t}");
            return this;
        }
        public Validator DateCompare(string rc, CompareType type, string errmsg, string successmsg)
        {
            WriteLine("\tif(_{0}.Success)", GetHashCode());
            WriteLine("\t{");
            WriteLine("\t\t_{0}.DateCompare($('#{1}').val(),$('#{2}').val(),'{3}');", GetHashCode(), Control, rc, type.ToString());
            WriteLine("\t\tif(!_{0}.Success)", GetHashCode());
            WriteLine("\t\t{");
            SetErrorMessage(errmsg);
            WriteLine("\t\t}");
            WriteLine("\t\telse{");
            SetSuccessMessage(successmsg);
            WriteLine("\t\t}");
            WriteLine("\t}");
            return this;
        }
        public Validator NumberCompare(string rc, CompareType type, string errmsg, string successmsg)
        {
            WriteLine("\tif(_{0}.Success)", GetHashCode());
            WriteLine("\t{");
            WriteLine("\t\t_{0}.NumberCompare($('#{1}').val(),$('#{2}').val(),'{3}');", GetHashCode(), Control, rc, type.ToString());
            WriteLine("\t\tif(!_{0}.Success)", GetHashCode());
            WriteLine("\t\t{");
            SetErrorMessage(errmsg);
            WriteLine("\t\t}");
            WriteLine("\t\telse{");
            SetSuccessMessage(successmsg);
            WriteLine("\t\t}");
            WriteLine("\t}");
            return this;
        }
        public Validator SelectCheckboxFromTo(string control, int from, int to, int selectcount, string msg, string successmsg)
        {
            WriteLine("\tif(_{0}.Success)", GetHashCode());
            WriteLine("\t{");
            WriteLine("\t\t_{0}.SelectCheckboxFromTo('{1}','{2}','{3}','{4}');", GetHashCode(), mControl, from, to, selectcount);
            WriteLine("\t\tif(!_{0}.Success)", GetHashCode());
            WriteLine("\t\t{");
            SetErrorMessage(msg);
            WriteLine("\t\t}");
            WriteLine("\t\telse{");
            SetErrorMessage(successmsg);
            WriteLine("\t\t}");
            WriteLine("\t}");
            return this;
        }
        public override string ToString()
        {
            return mScript.ToString() + "}\r\n";
        }
    }
    public enum CompareType
    {
        eq,
        neq,
        le,
        leq,
        ri,
        req
    }

}
